package cn.outsourcing.supermarket.provider;

import cn.outsourcing.supermarket.dto.GoodsListSearchDTO;
import org.apache.ibatis.jdbc.SQL;

/**
 * 商品 sql 构造器
 *
 * @author gzkemays
 * @since 2021/10/23 1:43
 */
public class GoodsProvider {
  private static final String PRICE_KEY = "price";
  private static final String SALES_KEY = "sales";

  public String getGoodsList(GoodsListSearchDTO entity) {
    String sort = entity.getSort();
    return new SQL() {
      {
        SELECT("*")
            .FROM("hiolabs_goods")
            .WHERE("is_delete = 0")
            .AND()
            .WHERE("name like concat('%',#{entity.keyword},'%')")
            .AND()
            .WHERE("is_on_sale = 1");
        if (PRICE_KEY.equals(sort)) {
          ORDER_BY("min_retail_price " + entity.getOrder());
        } else if (SALES_KEY.equals(sort)) {
          ORDER_BY("sell_volume " + entity.getSales());
        } else {
          ORDER_BY("min_retail_price " + entity.getOrder())
              .ORDER_BY("sell_volume " + entity.getSales());
        }
      }
    }.toString();
  }
}
